package com.lky.service.impl;

import com.lky.dao.UserDao;
import com.lky.dto.UserExecution;
import com.lky.entity.User;
import com.lky.service.UserService;
import com.lky.util.PageCalculator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Lenovo on 2018/1/16.
 */
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDao userDao;
    @Override
    public int addUser(User user) {
        return userDao.insertUser(user);
    }

    @Override
    public List<User> queryUser() {
        return userDao.queryUser();
    }

    @Override
    public List<User> queryUserByName(String username) {
        return userDao.queryUserByName(username);
    }

    @Override
    public User getByUserId(Integer userId) {
        return userDao.getByUserId(userId);
    }

    @Override
    public int updateUser(User user) {
        return userDao.updateUser(user);
    }

    @Override
    public int delByUserId(Integer userId) {
        return userDao.delByUserId(userId);
    }

    @Override
    public UserExecution getUserList(int pageIndex, int pageSize) {
        //将pageIndex转换为相应的rowIndex
        int rowIndex = PageCalculator.calculateRowIndex(pageIndex,pageSize);
        List<User> userList = userDao.queryUserList(rowIndex,pageSize);
        int count = userDao.selectCount();
        UserExecution ue = new UserExecution();
        //将查询到的用户表和用户数量存入ue
        if(userList!=null){
            ue.setUserList(userList);
            ue.setCount(count);
        }
        return ue;
    }



}
